﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinnesLayer.LogicBusiness;
using EntitiesLayer.Entities;
using System.Web.Services;
using System.Web.Script.Services;

namespace ClinicaSanPablo.login
{
    public partial class login : System.Web.UI.Page
    {
        private static System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtusuario.Text == "") { lblerror.Text = "Ingrese el Usuario"; return; }
                if (txtpass.Text == "") { lblerror.Text = "Ingrese una Contraseña"; return; }

                Usuariobl objBusiness = new Usuariobl();
                List<Usuario> lista = objBusiness.ValUsuario(txtusuario.Text, txtpass.Text);

                if (lblNroIntentos.Text == "3")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Main", "window.close();", true);
                }

                if (lista.Count > 0)
                {
                    foreach (var acn in lista)
                    {
                        Session["id_usuario"] = acn.VUSU_NRO_DOCUMENTO;
                        Session["Nombres"] = acn.Nombres + ", " + acn.Apellidos;
                        Session["Perfil"] = acn.IPER_ID_PERFIL;
                        Session["CodArea"] = acn.VARE_COD_AREA;
                        Session["DesArea"] = acn.VARE_DESC_AREA;
                        Session["CodSede"] = acn.VSED_COD_SEDE;
                        Session["DesSede"] = acn.VSED_DESC_SEDE;

                    }
                    Response.Redirect("../RegistroServicioNoConforme.aspx", true);

                }
                else
                {
                    lblerror.Text = "El usuario no es válido";
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "Main", "alert('El usuario no es válido')", true);
                    lblNroIntentos.Text = Convert.ToString(int.Parse(lblNroIntentos.Text == "" ? "0" : lblNroIntentos.Text) + 1);
                }


            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

    }
}